/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.ChaseCamera;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.InputListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;

/**
 *
 * @author Ruben
 */
public class Inputs {
   
   private SimpleApplication sa;
   private Node playerShip;
   private PlayerShipControl psc;
   private ChaseCamera chaseCam;
   Boolean isRunning=true;
   
   public Inputs (SimpleApplication sa, Node playerShip, PlayerShipControl psc, ChaseCamera chaseCame) {
       this.sa = sa;
       this.playerShip = playerShip;
       this.chaseCam = chaseCam;
       this.psc = psc;       
       setupKeys(this.sa.getInputManager());
       
   }

    private void setupKeys(InputManager inputManager)  {
        
        InputManager input = sa.getInputManager();
        
        // You can map one or several inputs to one named action
        input.addMapping("Pause",  new KeyTrigger(KeyInput.KEY_P));
        input.addMapping("Forward",  new KeyTrigger(KeyInput.KEY_W));
        input.addMapping("Backward", new KeyTrigger(KeyInput.KEY_S));
        input.addMapping("RotateR", new KeyTrigger(KeyInput.KEY_D));
        input.addMapping("RotateL", new KeyTrigger(KeyInput.KEY_A));

        input.addMapping("Shoot", 
            new KeyTrigger(KeyInput.KEY_SPACE),
            new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

        // Add the names to the action listener.
        input.addListener(actionListener, new String[]{"Pause","Shoot"});
        input.addListener(analogListener, new String[]{"Forward", "Backward", "RotateR", "RotateL"});
      }
        

         private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("Pause") && !keyPressed) {
        isRunning = !isRunning;
      }
      /*  if(name.equals("Shoot") && !keyPressed){
        CollisionResults results = new CollisionResults();
        
        Vector2f click2d = inputManager.getCursorPosition();
        Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
        Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();
        
        Ray ray = new Ray(click3d, dir);
        
        nav.collideWith(ray, results);

        for (int i = 0; i < results.size(); i++) {

            if (results.size() > 0) {
                CollisionResult closest = results.getClosestCollision();

                mark.setLocalTranslation(closest.getContactPoint());
                
                
                navFlag = 1;
                rotationFlag = 1;
                rootNode.attachChild(mark);
                
              Material bulletMaterial =  new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
              bulletMaterial.setColor("Color", ColorRGBA.Yellow);

            bulletIndex += 1;
            
            bulletObjList.add(bulletIndex,
                    (new Bullet(bulletMaterial, ship.getLocalTranslation(), dir)));

            rootNode.attachChild
                    (bulletObjList.get(bulletIndex).getSpatial());
            } else {
                rootNode.detachChild(mark);
            }
        }
        
        
    }*/
    }
  };
  
  private AnalogListener analogListener = new AnalogListener() {
    public void onAnalog(String name, float value, float tpf) {
      if (isRunning) {
        if (name.equals("RotateR")) {
          playerShip.rotate(0, -value*1*2, 0);
        }
        if (name.equals("RotateL")) {
          playerShip.rotate(0, value*1*2, 0);
        }
        if (name.equals("Backward")) {          
          //shipSpeed -= 1;
          //5ship.setLocalTranslation(v.x , v.y, v.z - value*speed*100);
        }
        if (name.equals("Forward")) {
          //shipSpeed += 1;
        }
      } else {
        //System.out.println("Press P to unpause.");
      }
    }
  };
}
